package cn.xmkeshe.controller;

import cn.xmkeshe.back.IAdminServiceBack;
import cn.xmkeshe.model.Admin;
import cn.xmkeshe.utils.Log;
import cn.xmkeshe.utils.md5.MD5Code;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

@Controller
public class AdminController {

  @Resource
  private IAdminServiceBack iAdminServiceBack;

//  回到系统首页
  @RequestMapping("/pages/back/admin/index")
  @Log(operationName = "回到系统首页",operationType = "管理员")
  public String index(){
    return "/pages/back/index";
  }


  //  分页查询管理员
  @RequestMapping("/pages/back/admin/listSplitPre")
  @Log(operationName = "分页查询管理员前",operationType = "管理员")
  public String listSplitPre() {
    return "/pages/back/admin/list";
  }

  @RequestMapping("/pages/back/admin/listSplit")
  @ResponseBody
  @Log(operationName = "分页查询管理员数据",operationType = "管理员")
  public Map<String, Object> listSplit(Admin admin, HttpServletRequest request) {
    int currentPage = 1;
    int lineSize = 10;
    try {
      currentPage = Integer.parseInt(request.getParameter("page"));
    } catch (Exception e) {
    }
    try {
      lineSize = Integer.parseInt(request.getParameter("limit"));
    } catch (Exception e) {
    }
    Map<String, Object> map = iAdminServiceBack.listSplit(currentPage, lineSize);
//    System.out.println(map);
    map.put("code", 0);//解析接口状态，必须这样写
    map.put("msg", "加载中");//解析提示文本
    map.put("data", map.get("allAdmin"));//    解析数据列表
    map.put("count", map.get("allCount"));//    解析数据长度
    return map;
  }

//  添加管理员
  @RequestMapping("/pages/back/admin/addPre")
  @Log(operationName = "添加管理员页面",operationType = "管理员")
  public String addPre() {
    return "/pages/back/admin/add";
  }

  @RequestMapping("/pages/back/admin/add")
  @ResponseBody
  @Log(operationName = "添加管理员",operationType = "管理员")
  public String add(Admin admin,HttpServletRequest request) {
    String flag = "error";
    Set<Integer> all = new HashSet<Integer>();
    String result[] = request.getParameterValues("rid");
    for (int x = 0; x < result.length; x++) {
      all.add(Integer.parseInt(result[x]));
    }
    admin.setPassword(new MD5Code().getMD5ofStr("{123456}" + admin.getAid())); // 密码进行加密处理;
    admin.setPhoto("/img/photo/users.jpg");
    if (this.iAdminServiceBack.insert(admin, all)) {
      flag = "success";
    }
    return flag;
  }

  //模糊查询
  @RequestMapping("/pages/back/Admin/findByName")
  @ResponseBody
  @Log(operationName = "模糊查询管理员",operationType = "管理员")
  public Map<String, Object> findByName(String name, HttpServletRequest request){
    int currentPage = 1;
    int lineSize = 10;
    try{
      currentPage = Integer.parseInt(request.getParameter("page"));
    }catch (Exception e){}
    try{
      lineSize = Integer.parseInt(request.getParameter("limit"));
    }catch (Exception e){}
    Map<String, Object> map = iAdminServiceBack.findByName(currentPage,lineSize,"%" + name + "%");
    map.put("code",0);//解析接口状态，必须这样写
    map.put("msg","加载中");//解析提示文本
    map.put("data",map.get("data"));//    解析数据列表
    map.put("count",map.get("count"));//    解析数据长度
    return map;
  }
}